﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BioBooking.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace BioBooking.Client.Screens
{
    /// <summary>
    /// Interaction logic for winMovies.xaml 
    /// </summary>
    public partial class winMovies : UserControl
    {
        BioBooking.Data.IRepository repository;
        ObservableCollection<Movie> allMovies;

        public winMovies()
            : this(null)
        {
            InitializeComponent();
        }

        public Wizard wizard { get; set; }

        public winMovies(Wizard wizard)
        {
            InitializeComponent();
            this.wizard = wizard;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                repository = Repository.GetRepository();
                BindMovies();
            }
        }

        private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            BindMovies();
        }

        private void BindMovies()
        {
            if (!calendar.SelectedDate.HasValue)
                return;

            List<Movie> movieList = repository.GetMoviesByShowingsDate(calendar.SelectedDate.Value);
            cbMovie.ItemsSource = movieList;
        }

        private void cbMovie_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!calendar.SelectedDate.HasValue)
                return;

            allMovies = new ObservableCollection<Movie>(repository.GetMoviesByShowingsDate(calendar.SelectedDate.Value));

            cbMovie.DataContext = allMovies;
            Movie mov = cbMovie.SelectedValue as Movie;

            if (mov != null)
            {
                lblActors.Content = mov.Actors;
                lblCountry.Content = mov.Country;
                lblDescription.Content = mov.Description;
                lblGenre.Content = mov.Genre;
                lblInstructor.Content = mov.Instructor;
                lblLanguage.Content = mov.Language;
                lblReleaseDate.Content = mov.ReleaseDate;
                lblTitle.Content = mov.Titel;
                lblRuntime.Content = mov.Runtime;
                lbTimeSlots.DataContext = repository.GetShowsFromShowtime(mov.Id);
            }
            else
            {
                lblActors.Content = "";
                lblCountry.Content = "";
                lblDescription.Content = "";
                lblGenre.Content = "";
                lblInstructor.Content = "";
                lblLanguage.Content = "";
                lblReleaseDate.Content = "";
                lblTitle.Content = "";
                lblRuntime.Content = "";
                lbTimeSlots.DataContext = "";
            }
        }

        private void btnBookShow_Click(object sender, RoutedEventArgs e)
        {
            Showing show = lbTimeSlots.SelectedValue as Showing;
            if (show != null)
            {
                wizard.InsertNextView(new BookingScreen(show, wizard));
            }
        }
    }
}
